/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package startupsolutions.mobilize;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ultimoistante
 */
public class AppHandlerServlet extends HttpServlet
	{

	// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
	/**
	 * Handles the HTTP
	 * <code>POST</code> method. 
	 * Equivalent to CREATE in the RESTful paradigm
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		// creates new object
		try
			{
			MobileApp app = new MobileApp("");
			if(app != null)
				{
				app.setTitle(request.getParameter("app_title"));
				app.setDescription(request.getParameter("app_description"));
				app.setAddress(request.getParameter("app_address"));
				app.setGeoposition(request.getParameter("app_geoposition"));
				app.setTelephone(request.getParameter("app_telephone"));
				app.setEmail(request.getParameter("app_email"));
				app.setWebsite(request.getParameter("app_website"));
				//
				pm.makePersistent(app);
				}
			}
		finally
			{
			// closing the persistence manager, the object is automatically stored
			pm.close();
			}
		}


	/**
	 * Handles the HTTP
	 * <code>GET</code> method.
	 * Equivalent to READ in the RESTful paradigm
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		PrintWriter out = response.getWriter();
		//response.setContentType("text/json");
		//
		try
			{
			PersistenceManager pm = PMF.get().getPersistenceManager();
			String appId = request.getParameter("id");
			// retrieves existing object
			MobileApp app = pm.getObjectById(MobileApp.class, Long.parseLong(appId));
			if(app != null)
				{
				String output = "{\"application\": {\"app_id\": \"" + app.getId().toString() + "\", \"app_name\": \"" + app.getTitle() + "\", \"app_description\": \"" + app.getDescription() + "\", \"app_address\": \"" + app.getAddress() + "\", \"app_geoposition\": \"" + app.getGeoposition() + "\", \"app_telephone\": \"" + app.getTelephone() + "\", \"app_email\": \"" + app.getEmail() + "\", \"app_website\": \"" + app.getWebsite() + "\"}}";
				output = output.replace("\\", "");
				System.out.println(output);
				out.println(output);
				}
			}
		finally
			{			
			out.close();
			}
		}

	/**
	 * Handles the HTTP
	 * <code>PUT</code> method. 
	 * Equivalent to UPDATE in the RESTful paradigm
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Access-Control-Allow-Origin", "*");
		System.out.println("*** doPut >>>");
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String appId = request.getParameter("id");
		if(appId != null && !appId.equals(""))
			{
			System.out.println("*** doPut - id: " + appId);
			// retrieves existing object
			MobileApp app = pm.getObjectById(MobileApp.class, Long.parseLong(appId));
			try
				{
				if(app != null)
					{
					app.setTitle(request.getParameter("app_title"));
					app.setDescription(request.getParameter("app_description"));
					app.setAddress(request.getParameter("app_address"));
					app.setGeoposition(request.getParameter("app_geoposition"));
					app.setTelephone(request.getParameter("app_telephone"));
					app.setEmail(request.getParameter("app_email"));
					app.setWebsite(request.getParameter("app_website"));
					}
				}
			finally
				{
				// closing the persistence manager, the object is automatically stored
				pm.close();
				}
			}
		else
			System.out.println("*** doPut - BAD id!");

		}

	
	/**
	 * Handles the HTTP
	 * <code>DELETE</code> method. 
	 * Equivalent to DELETE in the RESTful paradigm
	 *
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
		{
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String appId = request.getParameter("id");
		if(appId != null && !appId.equals(""))
			{
			try
				{
				// retrieves existing object
				MobileApp app = pm.getObjectById(MobileApp.class, Long.parseLong(appId));
				pm.deletePersistent(app);
				}
			finally
				{
				// closing the persistence manager, the object is automatically stored
				pm.close();
				}
			}
		}

	
	/**
	 * Returns a short description of the servlet.
	 *
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo()
		{
		return "Short description";
		}// </editor-fold>
	
	}
